﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_AssemblyCheckFunctions : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        public panel_AssemblyCheckFunctions()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.AssemblyCheckFunctions.DoubleClick += AssemblyCheckFunctions_DoubleClick;
            this.AssemblyCheckFunctions.PopupMenuShowing += AssemblyCheckFunctions_PopupMenuShowing;
            this.assemblyCheckFunctionsContextMenu.ItemClicked += assemblyCheckFunctionsContextMenu_ItemClicked;
        }

        ////////////////////////////////
        /// Call Edit Form
        ////////////////////////////////
        private void callEditForm()
        {
            Cursor currentCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;

            DataRow row = AssemblyCheckFunctions.GetFocusedDataRow();
            if ((row[0].ToString() == "1") || (row[0].ToString() == "2"))
            {
                MessageBox.Show("Cannot edit Serial or Comp fuction.");
            }
            else
            {
                frm_Edit_AssemblyCheckFunctions editForm = new frm_Edit_AssemblyCheckFunctions();
                editForm.func_id = row[0].ToString();
                editForm.checktype_id = row[1].ToString();
                if (editForm.LoadData())
                {
                    editForm.ShowDialog();
                    LoadData();
                }
                else
                {
                    editForm.Dispose();
                    MessageBox.Show("Cannot edit record.");
                }
            }

            Cursor.Current = currentCursor;
        }

        void assemblyCheckFunctionsContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.assemblyCheckFunctionsContextMenu.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callEditForm();
                    break;
                case "addSelection":
                    frm_Add_AssemblyCheckFunctions add_Form = new frm_Add_AssemblyCheckFunctions();
                    add_Form.ShowDialog();
                    LoadData();
                    break;                
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void AssemblyCheckFunctions_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.assemblyCheckFunctionsContextMenu.Show(MousePosition);
        }

        private void AssemblyCheckFunctions_DoubleClick(object sender, EventArgs e)
        {
            callEditForm(); 
        }
        
        //////////////////////////////////////////////////
        /// Load data from database to grid
        //////////////////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT t1.FunctionID, t1.CheckTypeID, t2.CheckTypeName, t1.FunctionName,"
                + " CASE t1.FunctionType WHEN  0 THEN 'Serial' WHEN 1 THEN 'Staff' WHEN 2 THEN 'Date'"
                + " WHEN 3 THEN 'Sub-Serial' ELSE 'Other' END AS FunctionType, t1.ShowForm, t1.DefaultValue"
                + " FROM [231_CheckFuntions] AS t1 INNER JOIN"
                + " [231_CheckType] AS t2 ON t1.CheckTypeID = t2.CheckTypeID"
                + " ORDER BY t2.CheckTypeName, t1.FunctionID";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridAssemblyCheckFunctions.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("FunctionID", typeof(int));
                newTable.Columns.Add("CheckTypeID", typeof(int));
                newTable.Columns.Add("CheckTypeName", typeof(string));
                newTable.Columns.Add("FunctionName", typeof(string));
                newTable.Columns.Add("FunctionType", typeof(string));
                newTable.Columns.Add("ShowForm", typeof(bool));
                newTable.Columns.Add("DefaultValue", typeof(string));
                gridAssemblyCheckFunctions.DataSource = newTable;
            }
            AssemblyCheckFunctions.Columns[0].Caption = "Function ID";
            AssemblyCheckFunctions.Columns[1].Caption = "Check ID";
            AssemblyCheckFunctions.Columns[2].Caption = "Check Type";
            AssemblyCheckFunctions.Columns[3].Caption = "Function Name";
            AssemblyCheckFunctions.Columns[4].Caption = "Function Type";
            AssemblyCheckFunctions.Columns[5].Caption = "Show On Form";
            AssemblyCheckFunctions.Columns[6].Caption = "Default Value";
            AssemblyCheckFunctions.Columns[1].Visible = false;
            AssemblyCheckFunctions.BestFitColumns();
        }

        private void bottonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_AssemblyCheckFunctions add_Form = new frm_Add_AssemblyCheckFunctions();
            add_Form.ShowDialog();
            LoadData();
        }

        private void bottonEdit_Click(object sender, EventArgs e)
        {
            callEditForm(); 
        }

        private void bottonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
